import {
	defineStore
} from "pinia"

import {
	ref
} from "vue"

import {
	loginByMobileApi,
	verifyCodeApi
} from "@/api/modules/login.js"


const useLoginStore = defineStore("login", () => {

	const code = ref("")
	const token = ref("")
	const recirectURL = ref("/pages/index/index")
	const openType = ref("switchTab")
	// 点击发送验证码
	const verifyCodeApiAction = async (data) => {
		const res = await verifyCodeApi(data)
		console.log(res);
		if (res.code === 10000) {
			code.value = res.code
		}
		return res
	}

	const loginByMobileApiAction = async (data) => {
		const res = await loginByMobileApi(data)
		if (res.code === 10000) {
			token.value = res.data.token

			// 跳转页面
			uni[openType.value]({
				url: recirectURL.value
			})

		}
		return res
	}

	return {
		token,
		recirectURL,
		openType,
		verifyCodeApiAction,
		loginByMobileApiAction
	}

}, {
	persist: {
		paths: ["token", "recirectURL", "openType"]
	},
})

export default useLoginStore